package top.rainf.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.rainf.service.ICityService;
import top.rainf.support.Result;
import top.rainf.domain.vo.City;

import java.util.List;

/**
 * @author 星环
 * @date Wed May 10 21:04:41 CST 2023
 * @description 城市的控制器类 由本人的生成器自动生成~
 */
@RestController
@RequestMapping("/city")
public class CityController {
    @Autowired
    ICityService cityService;
    /**
    * 获取全部城市的列表
    * @return 返回是否获取成功~
    */
    @RequestMapping("/list")
    public Result list(){
        List<City> list = cityService.getCityList();
        if(list == null){
            return Result.fail();
        }
        return Result.success().addObj("list",list);
    }


    @RequestMapping("/listByProvinceId")
    public Result listByProvinceId(Integer id){
        List<City> cityList = cityService.getCityList(id);
        return Result.success().addObj("list",cityList);
    }

    /**
    * 根据id获取一个城市
    * @return 返回是否获取成功~
    */
    @RequestMapping("/one")
    public Result getOneCity(int id){
        City city = cityService.getOneById(id);
        if(city == null){
                    return Result.fail();
        }
        return Result.success().addObj("data",city);
    }
    /**
    * 根据id删除一个城市
    ---------------------------
    * @param id 要删除的城市的id
    * @return 返回是否获取成功~
    */
    @RequestMapping("/deleteById")
    public Result deleteById(int id){
        boolean suc = cityService.deleteOneById(id);
        return suc ? Result.success() : Result.fail();
    }

    @RequestMapping("/deleteByIds")
    public Result deleteByIds(List<Integer> idList){
        boolean suc = cityService.deleteAll(idList);
        return suc ? Result.success() : Result.fail();
    }

    @RequestMapping("update")
    public Result update(City city){
        boolean suc = cityService.updateById(city);
        return suc ? Result.success() : Result.fail();
    }
}
